package threadexercise;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolDemo2 {
    public static void main(String[] args) {
        //声明一个线程池，里面有两个线程
       ExecutorService pool= Executors.newFixedThreadPool(2);
       for (int i=0;i<5;i++){
         Runnable runnable=  new Runnable(){

               @Override
               public void run() {
                   Thread t=Thread.currentThread();//获取执行当前方法的线程
                   System.out.println(t.getName()+"线程任务开始执行");
                   try {
                       Thread.sleep(5000);
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
                   System.out.println(t.getName()+"线程任务执行结束");
               }
           };
           System.out.println("指派一个线程任务给线程池");
           pool.execute(runnable);
       }
       pool.shutdown();
        System.out.println("线程池停止了");

    }
}
